#include "ComponentManager.h"

ComponentManager* ComponentManager::_instance = NULL;
ComponentManager* ComponentManager::getInstance()
{
    if(!_instance)
        _instance = new ComponentManager;
    return _instance;
}

ComponentManager::ComponentManager()
{
    //ctor
}

ComponentManager::~ComponentManager()
{
    //dtor
}

void ComponentManager::addComponent(int entity, int type)
{
    Component* component = NULL;
    if(type == cPosition)
    {
        component = new PositionComponent();
    }
    else if(type == cVelocity)
    {
        component = new VelocityComponent();
    }
    else if(type == cImage)
    {
        component = new ImageComponent();
    }
    else if(type == cPhysics)
    {
        component = new PhysicsComponent();
    }
    else if(type == cItem)
    {
        component = new ItemComponent();
    }
    else if(type == cCollision)
    {
        component = new CollisionComponent();
    }
    else if(type == cInput)
    {
        component = new InputComponent();
    }
    else if(type == cNpc)
    {
        component = new NpcComponent();
    }
    else if(type == cMap)
    {
        component = new MapComponent();
    }
    else if(type == cText)
    {
        component = new TextComponenet();
    }

    _componentList[entity].push_back(component);
}




void ComponentManager::deleteComponent(int entity, int type)
{
    for(int i = 0; i != _componentList[entity].size(); i++)
    {
        if(_componentList[entity][i]->type == type)
        {
            _componentList[entity].erase(_componentList.begin()+i);
        }
    }
}


void ComponentManager::deleteComponentList(int entity)
{
    for(int i = 0; i != _componentList[entity].size(); i++)
    {
        if(_componentList[entity][i]->type == type)
        {
            _componentList[entity].clear();
        }
    }
}

bool ComponentManager::hasComponent(int entity, int type)
{
    for(int i = 0; i != _componentList[entity].size(); i++)
    {
        if(_componentList[entity][i]->type == type)
        {
            return true;
        }
    }
    return false;
}

Component* ComponentManager::getComponent(int entity, int type)
{
    for(int i = 0; i < _componentList[entity].size(); i++)
    {
        if(_componentList[entity][i]->type == type)
        {
            return _componentList[entity][i];
        }
    }
    return NULL;
}


